<?php

namespace Modules\Car\Http\Controllers\Api;

use App\Exports\OnTheJobExport;
use App\Helpers\ResponseData;
use App\Models\AttendanceStatistics;
use App\Models\Shop\Category;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Maatwebsite\Excel\Facades\Excel;
use Modules\Car\Entities\CarOrder;

//出勤统计
class AttendanceStatisticsController extends Controller
{
    use ResponseData;
    public function list(AttendanceStatistics $model,Request $request){
        $size = $request->input('size',20);
        if($request->filled('start')&&$request->filled('end')){
            $model = $model->whereBetween('created_at',[$request->start,$request->end]);
        }
        if($request->filled('order_sn')){
            $model = $model->where('order_sn','like','%'.$request->order_sn.'%');
        }
        if($request->filled('plate_number')){
            $model = $model->where('plate_number','like','%'.$request->plate_number.'%');
        }
        if($request->filled('department')){
            $model = $model->where('department',$request->department);
        }
        if($request->filled('operator')){
            $model = $model->where('operator',$request->operator);
        }
        if($request->filled('is_store')){
            $model = $model->where('is_store',$request->is_store);
        }
        if($request->type=='export'){
            $list = [];
            $data = $model->get();
            foreach ($data as $k=>$v){
                $list[$k]['time'] = $v->day;
                $list[$k]['department'] = $this->getDepartment($v->department);
                $list[$k]['type'] = $this->getType($v->type);
                $list[$k]['operator'] = $this->getType($v->operator);
                $list[$k]['order_sn'] = $v->order_sn??'/';
                $list[$k]['plate_number'] = $v->plate_number;
                $list[$k]['work_overtime'] = $v->work_overtime==2?'加班':'/';
            }
            $count = $data->count();
            $header = ['时间','门店','类型','人员','订单号','车牌号','是否加班'];
            return Excel::download(new OnTheJobExport($list,$header,$count),'出勤统计.xlsx');
        }
        $fetch = $this->statistic($model,[1]); // 加班取车送车单，不要统计到送车取车的数量上
        $send = $this->statistic($model,[2]); // 加班取车送车单，不要统计到送车取车的数量上
        $workOvertime = $this->statistic($model,[6,7]);
        $workOvertimeFetch = $this->statistic($model,[6]); // 加班取车单
        $workOvertimeFend = $this->statistic($model,[7]); // 加班送车单
        $wbFetch = $this->statistic($model,[3,5]); // 维保取车
        $wbFend = $this->statistic($model,[4]); // 维保送车
        $clean = $this->statistic($model,8);
        $wb = $this->statistic($model,[3,4,5]);
        $model2 = clone $model;
        if($request->filled('type')){
            $model2 = $model2->whereIn('type',$request->type);
        }
        $data = $model2->orderBy('day','asc')->paginate($size);
        return $this->success(compact('data','fetch','send','workOvertime','workOvertimeFetch','workOvertimeFend','wbFetch','wbFend','clean','wb'));
    }
    // 获取车管员当日当月送取车数量
    // 1取车;2送车;3维修取车;4维修送车;5保养取车;6取车加班;7送车加班;8清理
    public function userData(AttendanceStatistics $model,Request $request){
        // 删除过期清洗单
        $this->deleteQxEndData();
        // start,end 当天
        // stsrtmo,endmo 当月
        $size = $request->input('size',20);
        if($request->filled('operator')){
            $model = $model->where('operator',$request->operator);
        }
        $daysc = $this->statisticuser($model,[2,4,7],[$request->start,$request->end]); // 当天送车数量
        $dayqc = $this->statisticuser($model,[1,3,5,6],[$request->start,$request->end]); // 当天取车数量
        $dayqx = $this->statisticuser($model,8,[$request->start,$request->end]); // 当天清洗数量
        $dayqxzp = $this->statisticuser($model,81,[$request->start,$request->end]); // 当天未上传清洗照片的数量
        $dayjb = $this->statisticuser($model,[6,7],[$request->start,$request->end]); // 当天加班数量
        $monthsc = $this->statisticuser($model,[2,4,7],[$request->stsrtmo,$request->endmo]); // 当月送车数量
        $monthqc = $this->statisticuser($model,[1,3,5,6],[$request->stsrtmo,$request->endmo]); // 当月取车数量
        $monthqx = $this->statisticuser($model,8,[$request->stsrtmo,$request->endmo]); // 当月清洗数量
        $monthqxzp = $this->statisticuser($model,81,[$request->stsrtmo,$request->endmo]); // 当月未上传清洗照片的数量
        $monthjb = $this->statisticuser($model,[6,7],[$request->stsrtmo,$request->endmo]); // 当月加班数量
        if ($request->filled('type')) {
            if ($request->filled('isday')) {
                $data = $model->whereBetween('created_at',[$request->start,$request->end])->whereIn('type',$request->type)->orderBy('id','desc')->paginate($size);
            } else {
                $data = $model->whereBetween('created_at',[$request->stsrtmo,$request->endmo])->whereIn('type',$request->type)->orderBy('id','desc')->paginate($size);
            }
            // if($request->type==[8]){
            //     info('只有8');
            // }
            return $this->success(compact('data','daysc','dayqc','dayqx','dayjb','monthsc','monthqc','monthqx','monthjb','dayqxzp','monthqxzp'));
        } else {
            return $this->success(compact('daysc','dayqc','dayqx','dayjb','monthsc','monthqc','monthqx','monthjb','dayqxzp','monthqxzp'));
        }
    }
    protected function deleteQxEndData(){
        $modeldata = AttendanceStatistics::query()->where('type', 8)->where('clieaner_can_time','<=',now())->get();
        foreach ($modeldata as $item) {
            $dataitem = AttendanceStatistics::query()->where('id', $item->id)->first();
            if($dataitem){
                $dataitem->delete();
            }
        }
    }
    protected function getDepartment($department){
        if($department=='w'){
            return '/';
        }else{
            return $department;
        }
    }
    protected function getType($type){
        switch ($type){
            case 1:
                $data = '取车';
                break;
            case 2:
                $data = '送车';
                break;
            case 3:
                $data = '维修取车';
                break;
            case 4:
                $data = '维修送车';
                break;
            case 5:
                $data = '保养取车';
                break;
            case 6:
                $data = '取车加班';
                break;
            case 7:
                $data = '送车加班';
                break;
            case 8:
                $data = '清理';
                break;
            default:
                $data = '取车';

        }
        return $data;
    }
    protected function statistic($model,$type){
        $data = clone $model;
        if($type==8){
            return $data->where('type',$type)->where('status','!=',2)->where('is_change_car','!=',2)->where('is_return','!=',2)->count();
        }else{
            return $data->whereIn('type',$type)->where('status','!=',2)->where('is_change_car','!=',2)->where('is_return','!=',2)->count();
        }

    }
    protected function statisticuser($model,$type,$times){
        $data = clone $model;
        if($type==81){
            return $data->whereBetween('created_at',$times)->where('type',8)->where('status','!=',2)->where('is_change_car','!=',2)->where('is_return','!=',2)->whereNull('clieaner_img')->count();
        }elseif($type==8){
            return $data->whereBetween('created_at',$times)->where('type',$type)->where('status','!=',2)->where('is_change_car','!=',2)->where('is_return','!=',2)->count();
        }else{
            return $data->whereBetween('created_at',$times)->whereIn('type',$type)->where('status','!=',2)->where('is_change_car','!=',2)->where('is_return','!=',2)->count();
        }

    }
    public function store(AttendanceStatistics $model,Request $request){
        // 如果当前订单已被取消，就提示前端
        // $orderdetail = CarOrder::query()->where('order_sn',$request->order_sn)->first();
        // if($orderdetail->status < 0){
        //     return $this->failed('当前订单已被取消，请刷新页面获取最新订单',201);
        // }
        $ishavedata = $model->where('type', $request->type)->where('order_sn', $request->order_sn)->first();
        // 如果已存在同类型数据，那就不继续保存
        if($ishavedata){
            return $this->success('保存成功');
        }
        $data = $request->all();

        $model->fill($data);
        $model->save();
        return $this->success('保存成功');
    }
    public function edit(AttendanceStatistics $model,Request $request){
        $data = $model->find($request->id);
        return $this->success($data);
    }
    public function updateByOrderSn(AttendanceStatistics $model,Request $request){
        $data = $model->whereIn('type',[2,7])->where('order_sn',$request->order_sn)->first();
        if($data){
            $arr = $request->all();
            // 如果这个出勤记录已经是加班单了，那就不改变加班单状态
            if($data->work_overtime==2){
                $arr['type'] = 7;
                $arr['work_overtime'] = 2;
            }
            $data->update($arr);
        }
        return $this->success('修改成功');
    }
    public function updateByOrderSnQh(AttendanceStatistics $model,Request $request){
        $data = $model->whereIn('type',[1,6])->where('order_sn',$request->order_sn)->first();
        if($data){
            $arr = $request->all();
            // 如果这个出勤记录已经是加班单了，那就不改变加班单状态
            if($data->work_overtime==2){
                $arr['type'] = 6;
                $arr['work_overtime'] = 2;
            }
            $data->update($arr);
        }
        return $this->success('修改成功');
    }
    public function update(AttendanceStatistics $model,Request $request){
        $data = $model->find($request->id);
        $arr = $request->all();
        if($request->filled('pid')&&$request->pid>0){
            $arr['level'] = 2;
        }
        if($data){
            $data->update($arr);
        }
        return $this->success('修改成功');
    }
    public function destory(AttendanceStatistics $model,Request $request){
        $data = $model->find($request->id);
        $data->delete();
    }
}
